﻿using UserMgr.Domain.ValueObjects;
using Zack.Commons;

namespace UserMgr.Domain.Entities;

public record User:IAggregateRoot
{
    public Guid Id { get; init; }
    public PhoneNumber PhoneNumber { get; private set; }
    private string? passwordHash;
    public UserAccessFail UserAccessFail { get; private set; }

    private User()
    {
    }

    public User(PhoneNumber phoneNumber)
    {
        Id = Guid.NewGuid();
        PhoneNumber = phoneNumber;
        UserAccessFail = new UserAccessFail(this);
    }

    public bool HasPassWord()
    {
        return !string.IsNullOrEmpty(passwordHash);
    }

    public void ChangePassword(string value)
    {
        if (value.Length <= 3)
            throw new ArgumentException("密码长度不能小于等于3");
        passwordHash = HashHelper.ComputeMd5Hash(value);
    }

    public bool CheckPassword(string password)
    {
        return passwordHash == HashHelper.ComputeMd5Hash(password);
    }

    public void ChangePhoneNumber(PhoneNumber phoneNumber)
    {
        PhoneNumber = phoneNumber;
    }
}